package org.budo.activiti.util;

import org.budo.support.lang.util.ThrowableUtil;

/**
 * @author lmw
 */
public class ActivitiErrorUtil {
    public static String getError(Throwable e) {
        String error = getPropertyRequiredError(e);
        if (null != error) {
            return error;
        }

        error = getEvaluatingExpressionError(e);
        if (null != error) {
            return error;
        }

        error = getCannotResolveIdentifierError(e);
        if (null != error) {
            return error;
        }

        String _error = e + "";
        int a = _error.indexOf("Data too long for column");
        if (a > -1) {
            return "错误：文本内容过长";
        }

        if (ThrowableUtil.stackTraceContains(e, "LongFormType.convertFormValueToModelValue")) {
            return "错误：请输入整数";
        }

        return null;
    }

    private static String getCannotResolveIdentifierError(Throwable e) {
        String error = e + "";
        int a = error.indexOf("Cannot resolve identifier");
        if (a < 0) {
            return null;
        }

        int b = error.indexOf("\r\n", a);
        if (b < 0) {
            return null;
        }

        String _err = error.substring(a, b);
        return _err;
    }

    private static String getEvaluatingExpressionError(Throwable e) {
        String error = e + "";
        int a = error.indexOf("org.activiti.engine.ActivitiException: Error while evaluating expression:");
        if (a < 0) {
            return null;
        }

        int b = error.indexOf("\r\n");
        if (b < 0) {
            return null;
        }

        String _err = error.substring(a + 39, b);
        return _err;
    }

    private static String getPropertyRequiredError(Throwable e) {
        String error = e + "";
        int a = error.indexOf("org.activiti.engine.ActivitiException: form property");
        if (a < 0) {
            return null;
        }

        int b = error.indexOf("is required");
        if (b < 0) {
            return null;
        }

        String _err = error.substring(a + 39, b + 11);
        return _err;
    }
}